I have an app with a web view in which I load HTML content with JavaScript enabled. The web view is inside a fragment.
This is how I initialize the web view inside the method onCreateView of the fragment :
WebView webview = (WebView) mainView.findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportMultipleWindows(true);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/*
* My code
*/
}
});
webview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
if (data != null) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
startActivity(browserIntent);
}
return false;
}
});
webview.loadDataWithBaseURL(baseUrl, htmlData, "text/html", "utf-8", "");
In the web view, a map is loaded with JavaScript. On this map, we can click on elements and load photos. When clicked, the photo is displayed in a popup (still inside the web view). When I click on the back button to go back to the map, the app crashes.
Here is the error log :
A/libc: Fatal signal 5 (SIGTRAP), code 1 in tid 949 (Chrome_InProcRe) [ 03-21 11:26:08.510 364: 364 W/ ] debuggerd: handling request: pid=32610 uid=10289 gid=10289 tid=949
I tested and got the crash on Android 7.1.1, 6.0.1, 5.0.2. Then I tried with Android 4.4.2 and the app didn't crash.
When I click on the back button (as we can see on the GIF), it should go back to the previous state with the popup closed
Try to override the back navigation functionality and close the popup yourself.
You don't need to handle all the stack navigation logic, just have a state when you are showing this popup. Apply your own navigation logic(like manually closing the popup).
void onBackPressed(){
if(isTheBuggyPopupIsOn){
closeTheBuggyPopup();
}
else{
super.onBackPressed();
}
}
Try this....
First add this
webView.setWebViewClient(new MyBrowser());
and then add this
public class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (url.equals(""YOUR URL)) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
} else {
return false;
}
}
}